package bookpad.page;

import java.util.List;

import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import scenic3.annotation.ActionPath;
import scenic3.annotation.Default;
import scenic3.annotation.Page;
import scenic3.annotation.Var;
import bookpad.model.UserBook;
import bookpad.service.AmazonService;
import bookpad.service.UserDataService;

@Page("/bookshelf")
public class BookShelfPage extends BasePage {

    AmazonService amazonService = new AmazonService();
    UserDataService userDataService = new UserDataService();

    @ActionPath("entry/{ASIN}")
    public Navigation entry(@Var("ASIN") String ASIN) {
        String status = requestScope("status");
        if (isNull(status))
            status = "to_read";
        UserBook ub = new UserBook();
        ub.setASIN(ASIN);
        ub.setStatus(status);
        Datastore.put(ub);
        return redirect(basePath);
    }

    @ActionPath("list/{status}")
    public Navigation list(@Var("status") String status) {
        List<UserBook> bookList =
            userDataService.getBooks(getUser().getUserId(), status);
        requestScope("bookList", bookList);
        requestScope("status", status);
        return forward("/bookshelf/list.jsp");
    }

    @Default
    public Navigation index() {
        return redirect("list");
    }
}
